package netective.soap;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.jws.WebService;

import org.jfree.chart.ChartUtilities;

import netective.api.analyzer.PacketType;
import netective.api.analyzer.PredictionType;
import netective.gui.charts.ChartGrain;
import netective.gui.charts.TimeChartFactory;
import netective.gui.charts.TimeSeriesChart;


@WebService(endpointInterface="netective.soap.SoapMessenger",serviceName="SoapMessenger")
public class SoapMessengerImpl implements SoapMessenger 
{
	@Override
	public String echo(String str) 
	{
		System.out.println("SoapMessenger#echo called");
		
		return str;
	}
	
	@Override
	public byte[] createClassesOfPacketsChart(ChartGrain chartGrain, 
			PredictionType predictionType, ArrayList<PacketType> packetTypes)
	{
		System.out.println("SoapMessenger#createClassesOfPacketsChart called");
		TimeSeriesChart chart = TimeChartFactory.classesOfPackets(chartGrain, predictionType, packetTypes);
		
		return convertChartToByteArray(chart);
	}
	
	@Override
	public byte[] createDirectionOfPacketsChart(ChartGrain chartGrain, 
			PredictionType predictionType, String protocols)
	{
		System.out.println("SoapMessenger#DirectionOfPackets called");
		TimeSeriesChart chart = TimeChartFactory.dirrectionOfPackets(chartGrain, predictionType, protocols);
		
		return convertChartToByteArray(chart);
	}
	
	@Override
	public byte[] createDownloadUploadChart(ChartGrain chartGrain, 
			PredictionType predictionType, String protocols)
	{
		System.out.println("SoapMessenger#DownloadUpload called");
		TimeSeriesChart chart = TimeChartFactory.downloadUpload(chartGrain, predictionType, protocols);
		
		return convertChartToByteArray(chart);
	}
	
	@Override
	public byte[] createDownloadUploadAverageChart(ChartGrain chartGrain, 
			PredictionType predictionType, String protocols)
	{
		System.out.println("SoapMessenger#DownloadUploadAverage called");
		TimeSeriesChart chart = TimeChartFactory.downloadUploadAverage(chartGrain, predictionType, protocols);
		
		return convertChartToByteArray(chart);
	}
	
	@Override
	public byte[] createOutPortChart(ChartGrain chartGrain, 
			PredictionType predictionType, String protocols, int top)
	{
		System.out.println("SoapMessenger#OutPort called");
		TimeSeriesChart chart = TimeChartFactory.outPort(chartGrain, predictionType, top, protocols);
		
		return convertChartToByteArray(chart);
	}
	
	@Override
	public byte[] createOtherPortChart(ChartGrain chartGrain, 
			PredictionType predictionType, String protocols, int top)
	{
		System.out.println("SoapMessenger#OtherPort called");
		TimeSeriesChart chart = TimeChartFactory.otherPort(chartGrain, predictionType, top, protocols);
		
		return convertChartToByteArray(chart);
	}
	
	@Override
	public byte[] createOtherAdressesChart(ChartGrain chartGrain, 
			PredictionType predictionType, String protocols, int top)
	{
		System.out.println("SoapMessenger#OtherAdresses called");
		TimeSeriesChart chart = TimeChartFactory.otherAdresses(chartGrain, predictionType, top, protocols);
		
		return convertChartToByteArray(chart);
	}
	
	private byte[] convertChartToByteArray(TimeSeriesChart chart)
	{
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		try 
		{
			ChartUtilities.writeChartAsJPEG(output, chart.getChart(), 
											chart.getWidth(), chart.getHeight());
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		
		return output.toByteArray();
	}
}
